<template>
<div>
  <!-- 面包屑导航 -->
  <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 20px;">
    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/website/config'}">网站管理</el-breadcrumb-item>
    <el-breadcrumb-item>评论管理</el-breadcrumb-item>
  </el-breadcrumb>

  <!--  列表数据  -->
  <el-table :data="list" border style="width: 100%" stripe>
    <el-table-column align="center" label="序号" width="60" type="index"></el-table-column>
    <el-table-column align="center" prop="commentUserAvatar" label="评论用户头像" width="120">
        <template slot-scope="scope">
          <el-image :src="scope.row.commentUserAvatar" style="width: 50px;height: 50px;border-radius: 50%"></el-image>
        </template>
    </el-table-column>
    <el-table-column align="center" prop="commentUserName" label="评论用户名" width="180"></el-table-column>
    <el-table-column align="center" prop="targetUserAvatar" label="被评论用户头像" width="120">
        <template slot-scope="scope">
          <el-image v-if="scope.row.targetUserAvatar" :src="scope.row.targetUserAvatar" style="width: 50px;height: 50px;border-radius: 50%"></el-image>
        </template>
    </el-table-column>
    <el-table-column align="center" prop="targetUserName" label="被评论用户名" width="180"></el-table-column>
    <el-table-column align="center" prop="content" label="评论内容" width="220"></el-table-column>
    <el-table-column align="center" prop="articleTitle" label="评论文章" width="240"></el-table-column>
    <el-table-column align="center" prop="createTime" label="评论时间" width="200" >
        <template slot-scope="scope">
          <!-- 格式化日期 -->
          {{getFormat(scope.row.createTime)}}
        </template>
    </el-table-column>
    <el-table-column  align="center" label="操作">
      <template slot-scope="scope">
        <el-button icon="el-icon-document-delete" type="danger" size="mini" @click="handleDel(scope.row.id)" >删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分页 -->
  <div class="block" style="margin-top: 10px;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="query.current"
      :page-size="query.size"
      background
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</div>
</template>

<script>
import {dateFormat} from '@/utils/date';
import commentApi from '@/api/blog.js';
export default {
  data() {
    return {
      list: [],
      query: {
        current: 1,
        size: 10
      },
      total: 0,
    }
  },

  created() {
    this.fetchData();
  },

  methods: {
    // 查询评论列表
    fetchData() {
      commentApi.queryCommentList(this.query).then(res => {
        if (res.code === 200) {
          this.list = res.data.list;
          this.total = res.data.total;
        }
      })
    },

    handleDel(id) {
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送删除请求
        commentApi.deleteComment(id).then(response => {
          if (response.code === 200) {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 1500
            });
          } else if (response.code === 306) {
            this.$notify.error({
              title: '错误',
              message: '您没有操作权限，请联系管理员',
              duration: 1500
            });
          } else {
            this.$notify.error({
              title: '错误',
              message: '系统异常，请稍后再试',
              duration: 1500
            });
          }
          // 刷新列表
          this.fetchData();
        })
      }).catch(() => {
        // 取消删除
      });
    },

    // val 是切换之后的每页显示多少条
    handleSizeChange(val) {
      this.query.size = val;
      this.fetchData();
    },

    // 当页码改变后触发此方法 val是当前点击（或输入）的页码
    handleCurrentChange(val) {
      this.query.current = val;
      this.fetchData();
    },

    // 格式化日期
    getFormat(date){
      return dateFormat(date);
    },
  },
}
</script>

<style scoped>

</style>
